// pages/my-likes/my-likes.js
import { matchApi } from '../../services/api';

Page({
  data: {
    likeList: [],
    loading: false,
    page: 1,
    pageSize: 10,
    hasMore: true
  },

  onLoad() {
    this.loadLikeList();
  },

  onPullDownRefresh() {
    this.setData({
      page: 1,
      likeList: [],
      hasMore: true
    });
    this.loadLikeList().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadLikeList();
    }
  },

  async loadLikeList() {
    if (this.data.loading) return;

    try {
      this.setData({ loading: true });

      const { list, total } = await matchApi.getLikes({
        page: this.data.page,
        pageSize: this.data.pageSize
      });

      console.log('获取到的收藏列表：', list);

      // 处理每个匹配对象的信息，确保数据格式正确
      const processedList = list.map(item => ({
        matchId: item.matchId,
        userInfo: {
          nickname: item.userInfo.nickname,
          age: item.userInfo.age,
          height: item.userInfo.height,
          education: item.userInfo.education,
          occupation: item.userInfo.occupation || '未填写',
          location: item.userInfo.location || '未填写',
          hobbies: item.userInfo.hobbies || '未填写',
          selfIntro: item.userInfo.selfIntro || '未填写',
          photos: item.userInfo.photos || [],
          avatarUrl: item.userInfo.avatarUrl,
          gender: item.userInfo.gender
        },
        createTime: this.formatTime(item.createTime)
      }));

      console.log('处理后的收藏列表：', processedList);

      this.setData({
        likeList: [...this.data.likeList, ...processedList],
        page: this.data.page + 1,
        hasMore: this.data.likeList.length + processedList.length < total
      });

    } catch (err) {
      console.error('获取收藏列表失败', err);
      wx.showToast({
        title: err.message || '获取收藏失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  formatTime(timeStr) {
    if (!timeStr) return '';
    const date = new Date(timeStr);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    const second = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  },

  handleViewDetail(e) {
    const { matchId } = e.currentTarget.dataset;
    const matchInfo = this.data.likeList.find(item => item.matchId === matchId);
    
    if (!matchInfo) {
      wx.showToast({
        title: '未找到匹配信息',
        icon: 'none'
      });
      return;
    }

    // 构建详情页需要的数据格式
    const detailInfo = {
      matchId: matchInfo.matchId,
      ...matchInfo.userInfo  // 展开userInfo中的所有字段
    };

    console.log('传递到详情页的数据：', detailInfo);
    
    const matchInfoStr = encodeURIComponent(JSON.stringify(detailInfo));
    wx.navigateTo({
      url: `/pages/match-result/match-result?matchInfo=${matchInfoStr}`
    });
  }
});